package com.nsgf.library.utils;


import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;


/**
 * 项目名称：组件化产品
 * 类名称：com.chinasoft.library.ui.controller.BaseController.java
 * 描述: 基础控制器，提供了一些基本的控制函数
 *
 * @author luwei
 * @version [版本号, 2016/10/17]
 */
public class DialogController {
    /**
     * 当前界面
     */
    private Dialog loading;
    private DialogUtil dialogUtil;
    private long lastLoadingTime = 0;
    private Runnable callBackAction;
    private Context context;
    private Handler handler;
    private DialogInterface.OnCancelListener onCancelListener;

    public DialogController(Context context) {
        this.context = context;
        handler = new Handler(Looper.getMainLooper());
        onCancelListener = null;
    }

    public void setOnCancelListener(DialogInterface.OnCancelListener onCancelListener) {
        this.onCancelListener = onCancelListener;
    }

    /**
     * 获取dialog工具集合
     *
     * @return DialogUtil
     * @author Desmond 2015-7-7 下午4:20:26
     */
    public DialogUtil getDialogs() {
        if (dialogUtil == null)
            dialogUtil = new DialogUtil(context);
        return dialogUtil;
    }

    /**
     * 获取loading
     *
     * @return ProgressDialog
     * @author Desmond 2015-7-7 下午4:20:35
     */
    private Dialog getLoading(final String text) {
        if (loading == null)
            loading = getDialogs().getLoadingDialog(text);
        else {
            getDialogs().setContent(loading, text);
        }
        if (onCancelListener != null) {
            loading.setCancelable(true);
            loading.setOnCancelListener(onCancelListener);
        } else {
            loading.setCancelable(false);
        }
        return loading;
    }

    /**
     * 从主线程{@link Looper}中移除回调
     */
    private void removeCallBackAction() {
        if (callBackAction != null) {
            handler.removeCallbacks(callBackAction);
        }
    }

    /**
     * 显示loading对话框
     */
    public void displayLoading() {
        displayLoading(null);
    }

    /**
     * 显示带文字的loading对话框
     *
     * @param text 显示的文字
     */
    public void displayLoading(final String text) {
        displayLoading(text, onCancelListener);
    }

    /**
     * 显示带文字的loading对话框
     *
     * @param text     显示的文字
     * @param listener 取消回调
     */
    public void displayLoading(final String text, final DialogInterface.OnCancelListener listener) {
        removeCallBackAction();
        lastLoadingTime = SystemClock.elapsedRealtime();
        handler.post(new Runnable() {
            @Override
            public void run() {
                Dialog dialog = getLoading(text);
                if (listener != onCancelListener) {
                    if (listener != null) {
                        dialog.setCancelable(true);
                        dialog.setOnCancelListener(listener);
                    } else {
                        dialog.setCancelable(false);
                    }
                } else {
                    dialog.setCancelable(true);
                }
                dialog.show();
            }
        });
    }

    /**
     * 取消loading对话框
     */
    public void dismissLoading() {
        dismissLoading(false);
    }

    /**
     * 取消loading对话框
     *
     * @param force 是否强制取消:
     *              true    立即dismiss掉loading对话框
     *              false   延迟一定时间{@link Constant#MinLoadingTime}后dismiss
     */
    public void dismissLoading(boolean force) {
        removeCallBackAction();
        if (force) {
            if (loading != null && loading.isShowing()) {
                loading.dismiss();
            }
        } else {
            if (loading != null && loading.isShowing()) {
                final long leftTime = SystemClock.elapsedRealtime() - lastLoadingTime;
                if (leftTime < 0) {
                    callBackAction = new Runnable() {
                        @Override
                        public void run() {
                            dismissLoading();
                        }
                    };
                    handler.postDelayed(callBackAction, leftTime);
                } else {
                    loading.dismiss();
                }
            }
        }
    }


    /**
     * 警告框
     *
     * @param msg
     * @param callback
     */
    public void alert(String msg, DialogInterface.OnClickListener callback) {
        alert(null, msg, "确定", callback);
    }

    public void alert(String title, String msg, String positive, DialogInterface.OnClickListener callback) {
        getDialogs().alert(title, msg, positive, null, callback);
    }

    public void alert(String title, String msg, String positive, String negative, DialogInterface.OnClickListener callback) {
        getDialogs().alert(title, msg, positive, negative, callback);
    }
}
